package com.hbisedm.interninfosys.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbisedm.interninfosys.commonutils.R;
import com.hbisedm.interninfosys.intern.entity.Notice;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.service.INoticeService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sam
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/intern/notice")
public class NoticeController {

    @Autowired
    IUserService userService;

    @Autowired
    INoticeService noticeService;

    @PostMapping("/list/{current}/{limit}")
    public R noticeList(@PathVariable long current,
                         @PathVariable long limit,
                         @RequestBody(required = false) Notice notice) {
        Page<Notice> noticePage = new Page<>(current, limit);
        noticeService.page(noticePage, null);
        List<Notice> records = noticePage.getRecords();
        for (Notice notice1 : records) {
            notice1.setUname(userService.getById(notice1.getUid()).getOthername());
            notice1.setUtype(userService.getById(notice1.getUid()).getType());
        }
        return R.ok().data("notices", records)
                .data("total",noticePage.getTotal());
    }

    @GetMapping("/info/{id}")
    public R noticeInfo(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        notice.setUname(userService.getById(notice.getUid()).getOthername());
        return R.ok().data("notice", notice);
    }

    @GetMapping("/toUndercarriage/{id}")
    public R toUndercarriage(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        notice.setStatus(3);
        boolean b = noticeService.updateById(notice);
        return R.ok().data("status", b);
    }
    @GetMapping("/toIssue/{id}")
    public R toIssue(@PathVariable Integer id) {
        Notice notice = noticeService.getById(id);
        notice.setStatus(2);
        boolean b = noticeService.updateById(notice);
        return R.ok().data("status", b);
    }

    @DeleteMapping("/delete/{id}")
    public R noticeDelete(@PathVariable Integer id) {
        boolean b = noticeService.removeById(id);
        if(b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/saveOrUpdate")
    public R noticeSaveOrUpdate(@RequestBody Notice notice) {
        boolean b = noticeService.saveOrUpdate(notice);
        if(b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/api/list")
    public R apiList() {
        List<Notice> list = this.noticeService.list(new QueryWrapper<Notice>().lambda().eq(Notice::getStatus, 2));
        list.forEach(item -> {
            User currentUser = userService.getById(item.getUid());
            item.setUtype(currentUser.getType());
            item.setUname(currentUser.getOthername());
        });
        return R.ok().data("noticeList", list);
    }

    @PostMapping("/api/add")
    public R apiAdd(@RequestBody Notice notice) {
        /**
         * 设置审核ing
         */
        notice.setStatus(1);
        boolean b = noticeService.saveOrUpdate(notice);
        return R.ok().data("status", b);
    }

    @PostMapping("/api/getList")
    public R apiGetList(@RequestBody Notice notice) {
        Integer uid = notice.getUid();
        List<Notice> list = noticeService.list(new QueryWrapper<Notice>().lambda().eq(Notice::getUid, uid));
        User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getId, uid));
        list.forEach(item -> {
            item.setUname(user.getOthername());
        });
        return R.ok().data("list", list);
    }
}
